package com.danco.training.bookshop.service.storage;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.danco.training.bookshop.comparator.ClaimComaratorByClaimAmount;
import com.danco.training.bookshop.comparator.ClaimComparatorByAlphabet;
import com.danco.training.bookshop.entity.Book;
import com.danco.training.bookshop.entity.Claim;

import org.apache.log4j.Logger;



import com.danco.training.bookshop.entity.storage.ClaimStorage;


public class ClaimStorageService implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4420530563741651021L;
	private ClaimStorage claimStorage;
	private BookStorageService bookStorageService;
	
	private static final Logger logger = Logger.getLogger(ClaimStorageService.class);
	private static ClaimStorageService claimStorageServiceInstance;
	
	private transient ClaimComparatorByAlphabet claimComparatorByAlphabet = new ClaimComparatorByAlphabet();
	private transient ClaimComaratorByClaimAmount claimComaratorByClaimAmount = new ClaimComaratorByClaimAmount();
	
	private ClaimStorageService() {

		this.claimStorage = ClaimStorage.getClaimStorageInstance();
		this.setBookStorageService(BookStorageService.getBookStorageSeviceInstance());
	}

	public List<Claim> getClaimList() {
		List<Claim> res = claimStorage.getClaims();
		logger.trace("Claim list");
		return res;

	}
	
	public List<Integer> getClaimListId(){
		List<Integer> res = new ArrayList<Integer>();
		for (Claim c :claimStorage.getClaims()) {
			res.add(c.getId())	;			
				}
			
		
		return res;
	
	}
	
	public int generateUnexistedId(){
		int i = 1;
		while(getClaimListId().contains(i)){
			i++;
		}
		return i;
	}
	
	
	public List<Claim> sortClaimsByAlphabet(List<Claim> claims) {
		Collections.sort(claims, claimComparatorByAlphabet);
		logger.trace("Claims are sorted by alphabet");
		return claims;

	}

	public List<Claim> sortClaimsByClaimAmount(List<Claim> claims) {
		Collections.sort(claims, claimComaratorByClaimAmount);
		logger.trace("Claims are sorted by the claim amoumt");
		return claims;
	}

	
	
	public Claim getClaimById(int id){
		for (Claim claim: claimStorage.getClaims()){
			if (claim.getId() == id){
				logger.trace("Claim is founded in Claimstorage by id");
				return claim;
			}
		}
		logger.info("Claim not founded in Claimstorage ");
		return null;
		
	}
	public boolean submitClaim(Claim claim) {
		
		
		if (getClaimById(claim.getId()) == null){
			return claimStorage.getClaims().add(claim);
			
		}
		
		else{
			logger.info("Claim not submitted");
			return false;
		}

	}
	
public boolean removeClaim(int id) {
		
	
		if (getClaimById(id) != null){
			return claimStorage.getClaims().remove(getClaimById(id));
		}
		return false;
	}

	



public static ClaimStorageService getClaimStorageServiceInstance() {
	if(claimStorageServiceInstance == null){
		claimStorageServiceInstance = new ClaimStorageService();
	}
	return claimStorageServiceInstance;
}

public BookStorageService getBookStorageService() {
	return bookStorageService;
}

public void setBookStorageService(BookStorageService bookStorageService) {
	this.bookStorageService = bookStorageService;
}


	
}
